package com.wgj.android.ui.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.google.gson.Gson;
import com.jiaai.xui.utils.http.HttpCallBack;
import com.jiaai.xui.utils.http.ListHttpCallBack;
import com.jiaai.xui.utils.http.XHttp;
import com.jiaai.xui.view.widget.XToast;
import com.jiaai.xui.view.widget.XTopbarView;
import com.jiaai.xui.view.widget.loadingview.XLoadingView;
import com.wgj.android.R;
import com.wgj.android.adapter.MessageContentAdapter;
import com.wgj.android.entity.Message;
import com.wgj.android.http.WGJAPI;
import com.wgj.android.utils.CacheUtils;
import com.wgj.android.utils.DateUtils;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MessageContentActivity extends AppCompatActivity {
    @BindView(R.id.listView)
    RecyclerView listView;
    @BindView(R.id.loadingView)
    XLoadingView loadingView;
    @BindView(R.id.XView)
    XTopbarView XView;
    private MessageContentAdapter adapter;
    private List<Message> list = new ArrayList<Message>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_description);
        ButterKnife.bind(this);
        initView();
        initDate();
    }

    private void initDate() {
        /*list = (List<Message>) getIntent().getSerializableExtra("messages");
        getIntent().getIntExtra("type",0);
        for(int i=0;i<list.size();i++){
            if( list.get(i).getType()==getIntent().getIntExtra("type",0)){
                messages.add(list.get(i));
            }
        }*/
        if(getIntent().getIntExtra("tag",0)==0){
            getMessageType(getIntent().getIntExtra("notifi",0));
        }else if(getIntent().getIntExtra("tag",0)==1){
            getMessageType(getIntent().getIntExtra("type",0));
        }



    }
    private void initView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        listView.setLayoutManager(layoutManager);
    }
    public void getMessageType(final int type){
        XHttp.obtain().get(WGJAPI.MSG_URL + "?mId=" + CacheUtils.getMember().getM_id()+"&typeId="+ type, null, new ListHttpCallBack<Message>() {
            @Override
            public void onSuccess(List<Message> messages) {

                    if(type==0){
                        XView.setTitle("系统通知");
                        setLookedMessage(0);
                    }else if(type==1){
                        XView.setTitle("我的钱包");
                        setLookedMessage(1);
                    }else if(type==2){
                        XView.setTitle("订单和提现");
                        setLookedMessage(2);
                    }

                    adapter = new MessageContentAdapter(MessageContentActivity.this,messages);
                    listView.setAdapter(adapter);
                    loadingView.showContent();


            }

            @Override
            public void onFailed(int i, String s, boolean b) {
                XToast.error("网络异常");
            }
        });
    }
    //设置为已读消息
    public void setLookedMessage(int type ){
        Map params = new HashMap<String,Object>();
        params.put("mId",CacheUtils.getMember().getM_id());
        params.put("typeId",type);
        XHttp.obtain().post(WGJAPI.LOOKED_MESSAGE_URL, params, new HttpCallBack<String>() {
            @Override
            public void onSuccess(String string) {

            }

            @Override
            public void onFailed(int i, String s, boolean b) {
                //XToast.error("网络异常");
            }
        });
    }
}
